package com.gopi.remfilebrowser.gui;

import java.io.File;

import com.gopi.utilities.gui.NewAbstractTreeNode;

public class FileBrowserTreeNode extends NewAbstractTreeNode implements Comparable<FileBrowserTreeNode>
{

	/*public static final int HOST_NODE = 1;
	public static final int DRIVE_NODE = 3;
	public static final int DIR_NODE = 4;
	public static final int FILE_NODE = 5;*/
	
	public static final int HOST_ROOT_UNCONNECTED = 0; 
	public static final int HOST_ROOT_CONNECTED = 1;
	public static final int LOCAL_HOST = 2;	
	public static final int DRIVE = 3;
	public static final int DIR = 4;
	public static final int FILE = 5;	
	public static final int LOADING = 6;
	
	public static final String HOST_CON_KEY = "host";
	public static final String HOST_UNCON_KEY = "host";
	public static final String DRIVE_KEY = "disk";
	public static final String DIR_KEY = "dir";
	public static final String FILE_KEY = "file";
	public static final String LOADING_KEY = "loading";
	public static final String LOCAL_HOST_KEY = "host";
	
	public static final String[] ICON_KEY= {HOST_CON_KEY,HOST_UNCON_KEY,LOCAL_HOST_KEY,DRIVE_KEY,DIR_KEY,FILE_KEY,LOADING_KEY};
	
	public static final String[] MAPPING = {"HOST_ROOT_UNCONNECTED","HOST_ROOT_CONNECTED","LOCAL_HOST",
		"DRIVE","DIR","FILE"};
	
	private int nodeType;
	private String fileName;
	private boolean isDirty = true;
	
	public FileBrowserTreeNode()
	{
		this(LOADING);
	}
	
	public FileBrowserTreeNode(int type)
	{
		setNodeType(type);
	}
	
	public boolean isDirty() 
	{
		return isDirty;
	}

	public void setDirty(boolean isDirty) 
	{
		this.isDirty = isDirty;
	}

	@Override
	public String getIconKey() 
	{
		return getIconKey(getNodeType());
	}
	
	@Override
	public String getIconKey(int status) 
	{
		return ICON_KEY[status];
	}
	
	public String toString()
	{
		switch(nodeType)
		{
		case HOST_ROOT_UNCONNECTED:
			return "Remote Machine (Not Connected)";
		case HOST_ROOT_CONNECTED:
			return "Remote Machine (Connected)";
		case DRIVE:
			//return "[Dr] " + fileName;
		case DIR:
			//return "[D] " + fileName;
		case FILE:
			return /*"[F] " + */fileName;
		case LOADING:
			return "   fetching ..."; 
		case LOCAL_HOST:
			return "Local Machine";
		}
		
		return "";
	}

	public int getNodeType() 
	{
		return nodeType;
	}

	public void setNodeType(int nodeType) 
	{
		this.nodeType = nodeType;
	}

	public int getNodeTypeForMenu()
	{
		return nodeType <=2 ? LOCAL_HOST : nodeType;
	}
	
	public int compareTo(FileBrowserTreeNode o) 
	{
		int level1 = new Integer(getNodeType()).compareTo(new Integer(o.getNodeType()));
		if(level1==0)
		{
			return fileName.compareTo(o.getFileName());
		}
		return level1;
	}

	public String getFilePath()
	{
		StringBuilder builder = new StringBuilder();
		FileBrowserTreeNode node = this;
		boolean first = true;
		while(node.getNodeType()>LOCAL_HOST)
		{
			System.out.println(node.getFileName());
			if(first)
			{
				builder.insert(0,node.getFileName());
				first = false;
			}
			else
			{
				builder.insert(0,node.getFileName() + (node.getFileName().endsWith(File.separator) ? "" : File.separator));
			}
			node = (FileBrowserTreeNode) node.getParent();
		}
		return builder.toString();
	}
	
	public boolean getAllowsChildren()
	{
		return getNodeType()!=FILE && getNodeType()!=LOADING;
	}
	
	public String getFileName() 
	{
		return fileName;
	}

	public void setFileName(String fileName) 
	{
		this.fileName = fileName;
	}
	
	public static int getMappingInt(String data)
	{
		int i = 0;
		for(String s : MAPPING)
		{
			if(s.equals(data))
				return i;
			i++;
		}
		return -1;
	}
}
